﻿using BaseUi.ViewModel;
using BaseUi.ViewModels;
//using HandyControl.Controls;
using LiveCharts.Dtos;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BaseUi.Views.MainWindows
{
    public class AnquanData : INotifyPropertyChanged
    {
        
        private SolidColorBrush anquanColor = MainWindow4.ytosafe;
        private string anquanCont = "";
        
        public string anquancont 
        {
            get { return anquanCont; }
            set
            {
                anquanCont = value;

                OnPropertyChanged("anquancont");
            }
        }
        public SolidColorBrush anquancolor
        {
            get { return anquanColor; }
            set
            {
                anquanColor = value;

                OnPropertyChanged("anquancolor");
            }
        }
        
        

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    /// <summary>
    /// MainWindow4.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow4 : Window
    {
        //public static AnquanData anqModel = new AnquanData();
        public static MainViewModel viewModel = MainWindow.viewModel;
        public static SolidColorBrush ytosafe = new SolidColorBrush();
        public SolidColorBrush RedColor = new SolidColorBrush(Color.FromRgb(255, 69, 0));
        public SolidColorBrush GreenColor = new SolidColorBrush(Color.FromRgb(173, 255, 47));
        public SolidColorBrush BoroColor = new SolidColorBrush(Color.FromRgb(220, 220, 220));
        public SolidColorBrush LemonChiffonColor = new SolidColorBrush(Color.FromRgb(255, 250, 205));
        public MainWindow4()
        {
            if (!viewModel.InitAgo())
                this.Close();

            InitializeComponent();

            viewModel.Init(this);
            this.DataContext = viewModel;
            //this.DataContext = anqModel;

        }

        private void load(object sender, RoutedEventArgs e)
        {
            MenuTest_MouseLeftButtonUp(selectBorder, null);
            Task wartocold = new Task(() =>
            {
                while (true)
                {
                    getytosafe();
                    getautomatic();
                    getsudu();
                    getanquna();
                    Thread.Sleep(200);
                }
            });
            wartocold.Start();
        }
        private void getanquna()
        {
            var saf = LocalStatic.Plc.Read<bool>("124");
            if (saf.IsSucceed == false)
            {
                viewModel.anquancolor = RedColor;
                viewModel.anquancont = "";
                return;
            }
            if (saf.Value)
            {
                viewModel.anquancolor = LemonChiffonColor;
                viewModel.anquancont = "ON";
            }
            else
            {
                viewModel.anquancolor = BoroColor;
                viewModel.anquancont = "OFF";
            }
        }
        /// <summary>
        /// Y轴到安全位
        /// </summary>
        private void getytosafe()
        {
            var saf = LocalStatic.Plc.Read<bool>("230");
            if (saf.IsSucceed == false)
            {
                viewModel.CommonViewModel.ytosafe = RedColor;
                return;
            }
            if (saf.Value)
            {
                viewModel.CommonViewModel.ytosafe = LemonChiffonColor;
            }
            else
            {
                viewModel.CommonViewModel.ytosafe = BoroColor;
            }
        }/// <summary>
         /// 自动
         /// </summary>
        private void getautomatic()
        {
            var saf = LocalStatic.Plc.Read<Int32>("1000");
            if (saf.IsSucceed == false)
            {
                return;
            }
            if (saf.Value==0)
            {
                viewModel.CommonViewModel.ischeck = true;
            }
            else
            {
                viewModel.CommonViewModel.ischeck = false;
            }
        }
        /// <summary>
        /// 速度
        /// </summary>
        /// <param name="inder"></param>
        private void getsudu()
        {
            var saf = LocalStatic.Plc.Read<Int32>("5");
            if (saf.IsSucceed == false)
            {
                getveloc(3);
                return;
            }
            getveloc(saf.Value);
        }
        private void getveloc(int inder)
        {
            viewModel.CommonViewModel.low = BoroColor;
            viewModel.CommonViewModel.intermediate = BoroColor;
            viewModel.CommonViewModel.high = BoroColor;
            if (inder == 0)
            {

                viewModel.CommonViewModel.low = GreenColor;
            }
            else if (inder == 1)
            {

                viewModel.CommonViewModel.intermediate = GreenColor;
            }
            else if (inder == 2)
            {

                viewModel.CommonViewModel.high = GreenColor;
            }
        }
        #region 菜单切换
        private void MenuTest_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            viewModel.SelectedValue = "校点";

            if (sender is Border selectBorder)
                if (!MenuSelect(selectBorder))
                    ChangeMenuColor(selectBorder);
        }

        private void MenuEdit_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            viewModel.SelectedValue = "轴控";

            if (sender is Border selectBorder)
                if (!MenuSelect(selectBorder))
                    ChangeMenuColor(selectBorder);
        }

        private void MenuSet_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            viewModel.SelectedValue = "信号";

            if (sender is Border selectBorder)
                if (!MenuSelect(selectBorder))
                    ChangeMenuColor(selectBorder);
        }
        private void Menujiaz_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            viewModel.SelectedValue = "夹爪";

            if (sender is Border selectBorder)
                if (!MenuSelect(selectBorder))
                    ChangeMenuColor(selectBorder);
        }
        #endregion

        #region 样式、效果

        //鼠标移动颜色
        SolidColorBrush leaveColor = new SolidColorBrush(Color.FromRgb(58, 144, 193));
        SolidColorBrush moveColor = new SolidColorBrush(Color.FromRgb(44, 177, 255));
        SolidColorBrush selectColor = new SolidColorBrush(Color.FromRgb(100, 185, 255));

        //菜单栏颜色切换
        private void ChangeMenuColor(Border selectBorder)
        {
            foreach (var item in StackPanelMenu.Children)
            {
                if (item is Border border)
                {
                    if (border == selectBorder)
                        border.Background = selectColor;
                    else
                        border.Background = leaveColor;
                }
            }
        }

        public bool MenuSelect(Border border)
        {
            return border.Background == selectColor;
        }

        //窗口移动事件
        private void TitleBar_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                this.DragMove();
            }
        }

        DateTime dts = DateTime.Now;
        //标题栏双击事件
        private void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton != MouseButtonState.Pressed)
                return;

            DateTime dtd = DateTime.Now;
            if ((dtd - dts).TotalMilliseconds <= 300)
                this.WindowState = this.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;

            dts = dtd;
        }

        private void BorderMin_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void BorderMax_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (this.WindowState == WindowState.Maximized)
            {
                this.WindowState = WindowState.Normal;
                winMax.Visibility = Visibility.Visible;
                winMin.Visibility = Visibility.Collapsed;
            }
            else
            {
                this.WindowState = WindowState.Maximized;
                winMax.Visibility = Visibility.Collapsed;
                winMin.Visibility = Visibility.Visible;
            }
        }

        private void BorderMenu_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            contextMenu.PlacementTarget = sender as Border;
            contextMenu.Placement = PlacementMode.Bottom;
            contextMenu.HorizontalOffset = 0;
            //显示菜单
            contextMenu.IsOpen = true;
        }

        private void BorderClose_MouseEnter(object sender, MouseEventArgs e)
        {
            if (sender is Border border)
                border.Background = Brushes.Tomato;
        }

        private void BorderClose_MouseLeave(object sender, MouseEventArgs e)
        {
            if (sender is Border border)
                border.Background = leaveColor;
        }
        private void BorderClose_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            MessageBoxResult result = HandyControl.Controls.MessageBox.Show("确定退出？", "退出提示", MessageBoxButton.OKCancel, MessageBoxImage.Question);
            if (result == MessageBoxResult.OK)
            {
                this.Close();
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void Menu_Leave(object sender, MouseEventArgs e)
        {
            if (sender is Border border)
                if (border.Background is SolidColorBrush solidColorBrush)
                    if (solidColorBrush.Color.R != selectColor.Color.R)
                        border.Background = leaveColor;
        }

        private void Menu_Enter(object sender, MouseEventArgs e)
        {
            if (sender is Border border)
                if (border.Background is SolidColorBrush solidColorBrush)
                    if (solidColorBrush.Color.R != selectColor.Color.R)
                        border.Background = moveColor;
        }

        private void tabControl_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            //取消tabControl快捷键切换
            if (e.Key == Key.LeftCtrl || e.Key == Key.LeftCtrl || e.Key == Key.Tab)
                e.Handled = true;
        }
        #endregion

        private void pButton_Clickstop(object sender, MouseButtonEventArgs e)
        {
            try
            {
                var ioi = LocalStatic.Plc.Write<bool>("122", true);
                if (ioi.IsSucceed == false)
                {
                    HandyControl.Controls.Growl.Error("停止plc命令dow上传失败");
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.ToString());
            }
        }
        private void UpButton_Clickstop(object sender, MouseButtonEventArgs e)
        {
            try
            {
                var ioi = LocalStatic.Plc.Write<bool>("122", false);
                if (ioi.IsSucceed == false)
                {
                    HandyControl.Controls.Growl.Error("停止plc命令up上传失败");
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.ToString());
            }
        }

        private void pButton_Clickgo(object sender, MouseButtonEventArgs e)
        {
            try
            {
                var ioi = LocalStatic.Plc.Write<bool>("123", true);
                if (ioi.IsSucceed == false)
                {
                    HandyControl.Controls.Growl.Error("复位plc命令dow上传失败");
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.ToString());
            }
        }
        private void UpButton_Clickgo(object sender, MouseButtonEventArgs e)
        {
            try
            {
                var ioi = LocalStatic.Plc.Write<bool>("123", false);
                if (ioi.IsSucceed == false)
                {
                    HandyControl.Controls.Growl.Error("复位plc命令Up上传失败");
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.ToString());
            }
        }
    
        private void pButton_Clickanq(object sender, RoutedEventArgs e)
        {
            try
            {
                var ioi = LocalStatic.Plc.Write<bool>("124", (sender as Button).Content == "OFF");
                if (ioi.IsSucceed == false)
                {
                    HandyControl.Controls.Growl.Error("安全屏蔽上传失败");
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.ToString());
            }
        }

        
    }
}
